package zz.app.ustbgsmanager.beans.queryscore;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

/**
 * Created by zz on 2018/6/3 20:22.
 */
public class Scores {
    private ArrayList<Score> scores;
    private float totalCredits;
    private float actualCredits;
    private String studentID;
    private String studentName;

    public String getStudentID() {
        return studentID;
    }

    public String getStudentName() {
        return studentName;
    }

    public Scores() {
        scores=new ArrayList<>();
    }

    public ArrayList<Score> getScores() {
        return scores;
    }


    public float getTotalCredits() {
        return totalCredits;
    }

    public float getActualCredits() {
        return actualCredits;
    }


    public static Scores getScores(String html){
        Scores scores=new Scores();
        try {
            Document document = Jsoup.parse(html);
            scores.studentID = document.getElementById("lblxh").text();
            scores.studentName = document.getElementById("lblxm").text();
            scores.actualCredits=Float.parseFloat(document.getElementById("lblyxxf").text());
            scores.totalCredits=Float.parseFloat(document.getElementById("lblyxxf1").text());
            Element scoreTable=document.getElementsByTag("table").get(6);
            Elements scoreTrs=scoreTable.getElementsByTag("tr");
            scoreTrs.remove(0);
            for (Element tr:scoreTrs) {
                Elements tds=tr.getElementsByTag("td");
                Score score=new Score();
                score.setCourseId(tds.get(0).text());
                score.setCourseName(tds.get(1).text());
                score.setCourseCredits(tds.get(2).text());
                score.setCourseTerm(tds.get(3).text());
                score.setCourseScore(tds.get(4).text());
                scores.getScores().add(score);
            }

        }catch (Exception ex){
            scores=null;
        }

        return scores;
    }
}
